package com.ychug.gateway.cms;

import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.business.consts.FrontConst;
import com.ychug.framework.business.dto.CmsNewsDTO;
import com.ychug.framework.business.service.CmsSiteService;
import com.ychug.framework.core.advice.AdDirective;
import com.ychug.framework.core.advice.NewsDataListDirective;
import com.ychug.framework.util.TempleteUtile;
import com.ychug.framework.web.base.BaseController;
import com.ychug.service.cms.entity.CmsCategoryEntity;
import com.ychug.service.cms.entity.CmsNewsDataEntity;
import com.ychug.service.cms.entity.CmsSiteEntity;
import com.ychug.service.cms.service.ICmsCategoryService;
import com.ychug.service.cms.service.ICmsNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台管理系统中生成静态页面
 * 2019-10-26
 * 杜东升
 */
@Controller
@RequestMapping("/cms/genStatic")
public class CmsGenStaticController extends BaseController {

    @Autowired
    private ICmsNewsService cmsNewsService;

    @Autowired
    private ICmsCategoryService cmsCategoryService;

    @Autowired
    private AdDirective adDirective;

    @Autowired
    private NewsDataListDirective newsDataListDirective;

    private String prefix = "modules/cms/genstatic";

    @GetMapping()
    public String genStatic()
    {
        return prefix + "/viewGenStatic";
    }



    @RequestMapping("/genNewsList")
    @ResponseBody
    public ResultAjaxHash genStaticNewsList(ModelAndView modelAndView, String catId, String contentId){
       CmsCategoryEntity cmsCategoryEntity = cmsCategoryService.selectCmsCategoryById(Integer.parseInt(catId));
        try {
            String newsTemplate = cmsCategoryEntity.getNewsPath();
            String rootOutPath = FrontConst.getTemplateGlobal_Pc_Out_Path();

            TempleteUtile templeteUtile = new TempleteUtile();
            Map data = new HashMap();
            data.put("ad",adDirective);
            data.put("newsList",newsDataListDirective);

            CmsSiteEntity cmsSiteEntity =  getCmsSiteEntity();
            data.put("cmsSite",cmsSiteEntity);
            data.put("siteUrl",cmsSiteEntity.getAccessPath());

            File file = new File(rootOutPath+newsTemplate);
            if(!file.exists()){
                file.mkdir();
            }
            File outFile = new File(rootOutPath+newsTemplate+".html");
            templeteUtile.process("news/news_list.ftl", data, outFile);
            System.out.println("恭喜，生成成功~~");

        }catch (Exception e){
            e.printStackTrace();
            return toAjax(0);
        }

        return toAjax(1);
    }


    @RequestMapping("/genNews")
    @ResponseBody
    public ResultAjaxHash genStaticNews(HttpServletRequest request, HttpServletResponse response , ModelAndView modelAndView, String catId, String contentId){
        CmsCategoryEntity cmsCategoryEntity = cmsCategoryService.selectCmsCategoryById(Integer.parseInt(catId));
        try {



            String newsTemplate = cmsCategoryEntity.getNewsPath();
            String rootOutPath = FrontConst.getTemplateGlobal_Pc_Out_Path();

            TempleteUtile templeteUtile = new TempleteUtile();
            Map data = new HashMap();

            CmsSiteEntity cmsSiteEntity =  getCmsSiteEntity();
            data.put("cmsSite",cmsSiteEntity);
            data.put("siteUrl",cmsSiteEntity.getAccessPath());

            request.setAttribute("contentId",contentId);
            request.setAttribute("sitId",cmsSiteEntity.getSiteId());

            data.put("ad",adDirective);
            data.put("newsList",newsDataListDirective);

            File file = new File(rootOutPath+newsTemplate);
            if(!file.exists()){
                file.mkdir();
            }

            //判断如果存在就删除该文件
            File file1 = new File(rootOutPath+newsTemplate+contentId+".html");
            if(file1.exists()){
                file1.delete();
            }

            File outFile = new File(rootOutPath+newsTemplate+contentId+".html");
            templeteUtile.process("news/news_detail.ftl", data, outFile);
            System.out.println("恭喜，生成成功~~");

        }catch (Exception e){
            e.printStackTrace();
            return toAjax(0);
        }

        return toAjax(1);
    }


    /**
     * 生成首页
     * @param request
     * @param response
     * @param modelAndView
     * @param cateId
     * @param contentId
     * @return
     */
    @RequestMapping("/genNewsIndex")
    @ResponseBody
    public ResultAjaxHash genStaticNewsIndex(HttpServletRequest request, HttpServletResponse response , ModelAndView modelAndView, String cateId, String contentId){

        try {

            String rootOutPath = FrontConst.getTemplateGlobal_Pc_Out_Path();

            TempleteUtile templeteUtile = new TempleteUtile();
            Map data = new HashMap();

            CmsSiteEntity cmsSiteEntity =  getCmsSiteEntity();
            data.put("cmsSite",cmsSiteEntity);
            data.put("siteUrl",cmsSiteEntity.getAccessPath());

            data.put("ad",adDirective);
            data.put("news_list",newsDataListDirective);

            File file = new File(rootOutPath);
            if(!file.exists()){
                file.mkdir();
            }

            //判断如果存在就删除该文件
            File file1 = new File(rootOutPath+"index.html");
            if(file1.exists()){
                file1.delete();
            }

            File outFile = new File(rootOutPath+"index.html");
            templeteUtile.process("index.ftl", data, outFile);
            System.out.println("恭喜index.ftl，生成成功~~");

        }catch (Exception e){
            e.printStackTrace();
            return toAjax(0);
        }

        return toAjax(1);
    }


}
